package com.SpringRSSReader;

//import java.text.SimpleDateFormat;
import java.util.ArrayList;
//import java.util.Date;
//import java.util.Iterator;

import android.app.Activity;
//import android.app.AlertDialog;
//import android.app.Dialog;
import android.content.Intent;
//import android.database.Cursor;
import android.os.Bundle;
//import android.view.LayoutInflater;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
//import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;


public class ShowListActivity extends Activity {
	public static RssItem selectedRssItem = null;
	private static final String DEBUG_TAG="SPRING_TAG";
	private static String feedUrl = "";
	ListView rssListView = null;
	ArrayList<RssItem> rssItems = new ArrayList<RssItem>();
	ArrayAdapter<RssItem> aa = null;
	
	public static final int RssItemDialog = 1;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_item);
        Log.i(DEBUG_TAG,"RSS_Reader Create");
 /*       
        // get button from layout.xml
        Button fetchRss = (Button) findViewById(R.id.fetchRss);

        // define the action that will be executed when the button is clicked.
        fetchRss.setOnClickListener(new OnClickListener() {
        	
	        @Override
	        public void onClick(View v) {
		        //Log.i(DEBUG_TAG,"URL: "  + feedUrl);
		        refreshRssList();
	        }
        });
*/      
        // get the listview from layout.xml
        rssListView = (ListView) findViewById(R.id.rssListView);
        // here we specify what to execute when individual list items clicked
        rssListView.setOnItemClickListener(new OnItemClickListener() {
        
	        @Override
	        public void onItemClick(AdapterView<?> av, View view, int index,
	        		long arg3) {
	        selectedRssItem = rssItems.get(index);
	        
	        // we call the other activity that shows a single rss item in
	        // one page
	        Intent intent = new Intent(
	        	"com.SpringRSSReader.displayRssItem");
	        startActivity(intent);
	        }
        });
        
        //adapters are used to populate list. they take a collection,
        //a view (in our example R.layout.list_item
        aa = new ArrayAdapter<RssItem>(this, R.layout.list_item, rssItems);
        //here we bind array adapter to the list
        rssListView.setAdapter(aa);
        refreshRssList();        
    }

    // for OptionMenu
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	menu.add(0,0,0,"Refresh")
    		.setIcon(R.drawable.ic_menu_refresh);
    	return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
    	Log.i(DEBUG_TAG,"Option selected: "+ item.getItemId());
    	switch(item.getItemId()) {
	    	case (0): refreshRssList();
	    		break;
	    	default: break;
    	}
    	return true;
    }
    // for refresh rsslist
	private void refreshRssList() {
		// TODO Auto-generated method stub
		Log.i(DEBUG_TAG,"Refresh RSS List");
		feedUrl = getResources().getString(R.string.rss_source);
		ArrayList<RssItem> newItems = RssItem.getRssItems(feedUrl);
		rssItems.clear();
		rssItems.addAll(newItems);
		aa.notifyDataSetChanged();
	}
}
